<?php
/**
 * Created by PhpStorm.
 * User: aiChenK
 * Date: 2019-01-03
 * Time: 11:27
 */
namespace Services\Msg;

use Helpers\Exception\HttpException;
use Helpers\HttpClient;
use Helpers\Main;
use Services\Msg\Core\Notice\Creator as NoticeCreator;
use Services\Msg\Core\Notice\Finder as NoticeFinder;
use Services\Msg\Core\Notice\Operator as NoticeOperator;
use Services\Msg\Core\Task\Creator as TaskCreator;
use Services\Msg\Core\Task\Finder as TaskFinder;
use Services\Msg\Core\Task\Operator as TaskOperator;

class Msg
{

    /**
     * 发送短信
     *
     * @param $phone
     * @param string $msg
     * @param string $module
     * @param string $subModule
     * @return bool
     * @throws HttpException
     *
     * create by ck 20190225
     */
    public static function sendSMS($phone, string $msg, $module = '', $subModule = '')
    {
        $url  = Main::getConfigByName('message')['url'];
        $data = [
            'clientId'         => $phone,
            'payload'          => $msg,
            'appId'            => 'platform',
            'sourceAppId'      => $module,
            'sourceInstanceId' => $subModule,
            'type'             => 'SMS',
            'channelId'        => 'SMS_SHIYUAN'
        ];
        $http = new HttpClient($url);
        $response = $http->postJson('api/v1/messages', $data);
        if (!$response->isSuccess()) {
            throw new HttpException('短信发送失败', $response);
        }
        $body = $response->toObject();
        if ($body->code != 'OK') {
            throw new HttpException($body->msg, $response);
        }
        return true;
    }

    /**
     * 获取消息创建类
     *
     * @param $module
     * @param $subModule
     * @param int $extraId
     * @return NoticeCreator
     *
     * create by ck 20190118
     */
    public static function noticeCreator(string $module, string $subModule, $extraId = 0)
    {
        return new NoticeCreator($module, $subModule, $extraId);
    }

    /**
     * 获取消息查找类

     * @return NoticeFinder
     *
     * create by ck 20190212
     */
    public static function noticeFinder()
    {
        return new NoticeFinder();
    }

    /**
     * 获取消息操作类
     *
     * @return NoticeOperator
     *
     * create by ck 20190212
     */
    public static function noticeOperator()
    {
        return new NoticeOperator();
    }

    /**
     * 获取任务创建类
     *
     * @param $module
     * @param $subModule
     * @param int $extraId
     * @return TaskCreator
     *
     * create by ck 20190118
     */
    public static function taskCreator(string $module, string $subModule, $extraId = 0)
    {
        return new TaskCreator($module, $subModule, $extraId);
    }

    /**
     * 获取任务查找类

     * @return TaskFinder
     *
     * create by ck 20190212
     */
    public static function taskFinder()
    {
        return new TaskFinder();
    }

    /**
     * 获取任务操作类
     *
     * @return TaskOperator
     *
     * create by ck 20190214
     */
    public static function taskOperator()
    {
        return new TaskOperator();
    }
}
